package com.jsgt.greekfire.ui.mine.user.securityset.bindphone

import android.os.CountDownTimer
import android.text.TextUtils
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.UserBean
import com.jsgt.greekfire.factory.UserViewModelFactory
import com.jsgt.greekfire.viewmodel.UserViewModel
import kotlinx.android.synthetic.main.activity_bind_phone.*
import kotlinx.android.synthetic.main.activity_bind_phone.bind_phone_code_et
import kotlinx.android.synthetic.main.activity_bind_phone.confirm_tv
import kotlinx.android.synthetic.main.activity_bind_phone.send_code_tv

/**
 * 绑定手机(原本有手机号就是修改)
 * author fhx
 * create time 2020/4/23 16:17
 **/
class BindPhoneActivity : BaseActivity() {
    private val userViewModel: UserViewModel by lazy {
        ViewModelProvider(
            this,
            UserViewModelFactory()
        ).get(UserViewModel::class.java)
    }
    private var countDown: CountDownTimer? = null
    override fun getLayoutId(): Int = R.layout.activity_bind_phone
    private var isChange:Boolean = false//是否是更换手机号
    override fun main() {
        if (TextUtils.isEmpty(UserBean.instance.mobile)) {
            title = getString(R.string.bind_phone)
        } else {
            isChange = true
            title = getString(R.string.change_phone)
        }
        confirm_tv.setOnClickListener {
            checkSmsCode()
        }
        send_code_tv.setOnClickListener {
            val phone = bind_phone_et.text.toString().trim()
            if (TextUtils.isEmpty(phone)) {
                ToastUtils.show(this, getString(R.string.bind_phone_input_phone))
            } else {
                if(isChange && phone == UserBean.instance.mobile){
                    ToastUtils.show(this, getString(R.string.bind_phone_input_phone_error))
                }else {
                    userViewModel.sendSMS(phone)
                }
            }
        }
        userViewModel.sendSmsLiveData.observe(this, Observer {
            ToastUtils.show(this, getString(R.string.send_sms_end))
            send_code_tv.isEnabled = false
            countDown = object : CountDownTimer(60000, 1000) {
                override fun onFinish() {
                    send_code_tv.text = getString(R.string.send_sms_code)
                    send_code_tv.isEnabled = true
                }

                override fun onTick(millisUntilFinished: Long) {
                    send_code_tv.text = "${(millisUntilFinished+500) / 1000}${getString(R.string.second)}"
                }
            }
            countDown?.start()
        })
        userViewModel.checkSMSLiveData.observe(this, Observer {
            val phone = bind_phone_et.text.toString().trim()
            val bean = UserBean.instance
            phonex = phone
            if(isChange) {
                userViewModel.modifyPhone(phone,bean.mobile!!)
            }else{
                userViewModel.bindPhone(phone)
            }
        })
        userViewModel.bindPhoneLiveData.observe(this, Observer {
            UserBean.instance.mobile = phonex
            ToastUtils.show(this, getString(R.string.complete))
            finish()
        })
        userViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
        })
    }
    var phonex:String? = null

    private fun checkSmsCode() {
        val phone = bind_phone_et.text.toString().trim()
        val code = bind_phone_code_et.text.toString().trim()
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.show(this, getString(R.string.bind_phone_input_phone))
            return
        }
        if(isChange && phone == UserBean.instance.mobile){
            ToastUtils.show(this, getString(R.string.bind_phone_input_phone_error))
            return
        }
        if (TextUtils.isEmpty(code)) {
            ToastUtils.show(this, getString(R.string.bind_phone_input_verification_code))
            return
        }
        showProgress(true)
        userViewModel.checkSMSCode(phone, code)
    }

    override fun onDestroy() {
        super.onDestroy()
        countDown?.cancel()
        countDown = null
    }
}